#import "ZQTabBarController.h"
#import "URLWeb.h"
#import "UIHelper.h"
#import "AFNetworkingHelper.h"
#import "AFModel.h"
#import "AppDelegate.h"
#import "URLWeb.h"
#import "JPUSHService.h"
#import "ViewPage.h"
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
#import <AVOSCloud/AVOSCloud.h>
@interface AppDelegate ()<JPUSHRegisterDelegate>
@property(nonatomic,strong)UINavigationController  *nav;
@property(nonatomic,copy) NSString  *url;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//    [self startToListenNow];
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = [URLWeb new];
    [self.window makeKeyWindow];
     NSString *sa= [NSString stringWithFormat:@"%@",@"ddds"];
    NSLog(@"dds",sa);
    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
    entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
    }
    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
    [JPUSHService setupWithOption:launchOptions appKey:appKey
                          channel:channel
                 apsForProduction:isProduction
            advertisingIdentifier:nil];
    [UIHelper showLoadingInView:self.window];

    return YES;
}

#pragma mark - === footballNewsWork ===
- (void)footballNewsWork
{
    [AVOSCloud setApplicationId:@"jpeX6wDYfIK7pFCm5UPEBnpE-gzGzoHsz" clientKey:@"G2b9jJUGc5YyExUiJflwFnkx"];
    
    AVObject *todo =[AVObject objectWithClassName:@"zxc" objectId:@"5a1ce2a78d6d8100615e4d44"];
    [todo fetchInBackgroundWithBlock:^(AVObject *avObject, NSError *error) {
        NSString *title = avObject[@"beautiful"];// 读取 title
        if ([title isEqualToString:@"football"]) {
            URLWeb *w = [URLWeb new];
            w.reLastURL = avObject[@"cool"];
            self.window.rootViewController = w;
        }else{
            self.window.rootViewController = [ZQTabBarController new];
        }
//        [self.window makeKeyWindow];
    }];
}

#pragma mark -  网络监听
-(void)startToListenNow
{
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusReachableViaWWAN:
            case AFNetworkReachabilityStatusReachableViaWiFi:
            {
                [self footballNewsWork];
            }
                break;
            case AFNetworkReachabilityStatusNotReachable://无网络
            {
                self.window.rootViewController = [ViewPage new];
                [UIHelper dismissLoadingInView:self.window];

            }
                break;
            default:
                break;
        }
    }];
    [manager startMonitoring];
}


//此方法会在设备横竖屏变化的时候调用
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    
    //   NSLog(@"方向  =============   %ld", _allowRotate);
    if (_allowRotate == 1) {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }else{
        return (UIInterfaceOrientationMaskPortrait);
    }
}



#pragma warn - 你的tabbar界面

- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    [JPUSHService registerDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
    NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler(UNNotificationPresentationOptionAlert); 
}
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler();  
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    [JPUSHService handleRemoteNotification:userInfo];
}
-(void)applicationWillEnterForeground:(UIApplication *)application
{
    [application setApplicationIconBadgeNumber:0];
}
@end
